Commonly Used String Functions in MySQL
MySQL provides many built-in string functions used to manipulate, search, extract, and format text data. Below are some of the most commonly used ones with examples.
UPPER(str) – Converts text to uppercase.
LOWER(str) – Converts text to lowercase.
LENGTH(str) – Returns the length in bytes.
CHAR_LENGTH(str) – Returns the length in characters.
SUBSTRING(str, start, length) – Extracts part of a string.
CONCAT(str1, str2, ...) – Joins strings.
CONCAT_WS(separator, str1, str2) – Joins with a separator.
REPLACE(str, from_str, to_str) – Replaces text.
TRIM(str) – Removes leading and trailing spaces.
LTRIM(str) – Removes left spaces.
RTRIM(str) – Removes right spaces.
LPAD(str, length, padstr) – Pads on the left.
RPAD(str, length, padstr) – Pads on the right.
INSTR(str, substring) – Returns position of substring.
LOCATE(substring, str) – Similar to INSTR, supports starting position.
LEFT(str, n) – Returns first n characters.
RIGHT(str, n) – Returns last n characters.
These string functions help perform efficient text processing in MySQL queries, making them essential tools for data manipulation.